﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SaleWebsite.library;
using System.Data;

namespace SaleWebsite.Models
{
    public class Provider
    {
        public ulong Id{get;set;}
        public string Address{get;set;}
        public string Phone{get;set;}
        public string Id_cart{get;set;}
        public string Email{get;set;}
        public string Name{get;set;}

        public ulong Insert()
        {
            
           try
           {
               return MySqlImplement.GetInstance().ExcuteQuery(Params.GetAllParams(this), "provider_insert");
           }
           catch (System.Exception ex)
           {
           	throw ex;
           }
           
        }
        public static DataSet GetAll()
        {
            return MySqlImplement.GetInstance().GetQuery("provider_getall");
        }
        public  int Delete()
        {
            return MySqlImplement.GetInstance().ExcuteCommand(Params.GetAllParams(this),"provider_delete");
        }
        public int Save()
        {
          try
          {
              return MySqlImplement.GetInstance().ExcuteCommand(Params.GetAllParams(this), "provider_update");
          }
          catch (System.Exception ex)
          {
          	throw ex;
          }
        }
        public DataSet GetGroupById()
        {
            return MySqlImplement.GetInstance().GetQuery("group_get_id", Params.GetAllParams(this));
        }
        private static Provider Convert_one(DataRow row)
        {
            return new Provider()
            
            {
              Name=row["name"].ToString(),
              Id=ulong.Parse(row["id"].ToString()),
              Email=row["email"].ToString(),
              Id_cart=row["id_cart"].ToString(),
              Phone=row["phone"].ToString(),
              Address=row["address"].ToString()
            };
        }
        public static IEnumerable<Provider>ConvertAll(DataSet data)
        {
            foreach (DataRow row in data.Tables[0].Rows)
            {
                yield return Convert_one(row);
            }
        }

    }
}